package com.financing.wap.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.financing.wap.common.SqlName;
import com.financing.wap.dao.IUserDao;
import com.financing.wap.dto.AuthenticationDTO;
import com.financing.wap.dto.CorpFaUserDTO;
import com.financing.wap.dto.FaUserCertDTO;
import com.financing.wap.dto.FaUserDTO;
import com.financing.wap.dto.FaUserWechatDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.vo.AdQueryVo;

@Repository("userDao")
@SuppressWarnings(value={"unchecked", "deprecation"})
public class UserDaoImpl extends BaseDaoImpl<UserDTO, java.lang.Integer> implements IUserDao {

	@Override
	public UserDTO getUserByUF(UserDTO user) {
		UserDTO userDTO = (UserDTO) this.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_NAMEPASSWORD, user);
		return userDTO;
	}

	/*
	 * FA注册
	 * @see com.financing.wap.dao.IUserDao#registerUser(com.financing.wap.dto.UserDTO)
	 */
	@Override
	public String registerUser(UserDTO user) {
		try{
			user.setId((String)this.sqlMapClientTemplate.queryForObject(SqlName.queryUUID));
			 this.sqlMapClientTemplate.insert(SqlName.USER_REGISTER, user);
			 return user.getId();
		}catch(Exception e){
			e.printStackTrace();
			log.error("error when register user!", e);
			return "";
		}
		
		
	}
	
	/*
	 * 判断手机号码是否被注册
	 * 如果已经被注册：返回true
	 * 如果没有被注册：返回false
	 * @see com.financing.wap.dao.IUserDao#isExistUserName(java.lang.String)
	 */
	@Override
	public boolean isMobileRegistered(String mobile) {
		
		String ID =  (String) super.sqlMapClientTemplate.queryForObject(SqlName.USER_ISEXIST,mobile);
		
		return !StringUtils.isEmpty(ID);
	}
	
	@Override
	public UserDTO queryUserByMobile(UserDTO user) {
		
		return (UserDTO) super.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_MOBILE,user);
		
	}
	@Override
	public UserDTO queryUserById(String userId) {
		
		return (UserDTO) super.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_BYID, userId);
		
	}
	@Override
	public UserDTO getUser(UserDTO user) throws Exception {
		
		return (UserDTO) super.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY, user);
	}

	
	@Override
	public CorpFaUserDTO getUserById(String userId){
		CorpFaUserDTO faUserDTO = (CorpFaUserDTO) this.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_USERID, userId);
		if(faUserDTO == null){
			return  null;
		}

		List<FaUserCertDTO> faUserCertDTOList = this.sqlMapClientTemplate.queryForList(SqlName.USER_CERT_QUERY_USERID, userId);
		AuthenticationDTO authenticationDTO = (AuthenticationDTO) this.sqlMapClientTemplate.queryForObject(SqlName.AUTHENTICATION_AUTH_QUERY,userId);
		
		//wap_jee_1.1 加入微信信息
		FaUserWechatDTO faUserWechatDTO =  new FaUserWechatDTO();
		faUserWechatDTO.setUserId(userId);
		FaUserWechatDTO view = (FaUserWechatDTO) this.sqlMapClientTemplate.queryForObject(SqlName.QUERY_USERWECHAT_INFO,faUserWechatDTO);
		if(view !=null){
		faUserWechatDTO.setNickname(view.getNickname());
		faUserWechatDTO.setHeadimgurl(view.getHeadimgurl());
		}
		
		
		faUserDTO.setAuthenticationDTO(authenticationDTO);
		faUserDTO.setFaUserCertDTOList(faUserCertDTOList);
		faUserDTO.setFaUserWechatDTO(faUserWechatDTO);
		return faUserDTO;
	}
	
	public String updateFaUserInfo(FaUserDTO dto) {
		if(StringUtils.isBlank(dto.getUpdateBy())){
			dto.setUpdateBy(StringUtils.isBlank(dto.getId())?dto.getuLoginName():dto.getId());
		}
		this.getSqlMapClientTemplate().update(SqlName.USER_UPDATE_USERINFO,dto);
		return dto.getId();
	}

	public int queryCountBySourceCode(String id) {
		Integer count = (Integer)this.getSqlMapClientTemplate().queryForObject(SqlName.USER_COUNT_SOURCE_CODE, id);
		return count;
	}
	
	
	@Override
	public Class<UserDTO> getEntityClass() {
		return UserDTO.class;
	}

	@Override
	public List<AdQueryVo> queryAdRegInfo(Date createDate) {
		return this.sqlMapClientTemplate.queryForList(SqlName.QUERY_AD_REG_INFO, createDate);
	}

	@Override
	public List<UserDTO> getInviteFaListByUserId(Map<String, Object> queryParams)
			throws Exception {
		return super.sqlMapClientTemplate.queryForList(SqlName.INVITE_USER_QUERY_BYID, queryParams);
	}

	@Override
	public int getInviteFaListByUserIdCount(UserDTO user) throws Exception {
		return (Integer) super.sqlMapClientTemplate.queryForObject(SqlName.INVITE_USER_QUERY_BYID_COUNT, user);
	}

	@Override
	public UserDTO queryUserLoginName(UserDTO user) {
		UserDTO userDTO = (UserDTO) this.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_LOGIN_NAME, user);
		return userDTO;
	}

	@Override
	public List<Map<String, Object>> getSFAList(Map<String, Object> queryParams)
			throws Exception {
		return (List<Map<String, Object>>) super.sqlMapClientTemplate.queryForList(SqlName.queryOrgUser, queryParams);
	}

	@Override
	public int getSFAListCount(Map<String, Object> queryParams)
			throws Exception {
		return (Integer) super.sqlMapClientTemplate.queryForObject(SqlName.queryOrgUserCount, queryParams);
	}

	@Override
	public Map<String, Object> getUser(Map<String, Object> queryParams)
			throws Exception {
		return (Map<String, Object>) super.sqlMapClientTemplate.queryForObject(SqlName.queryMapUser, queryParams);
	}

	@Override
	public UserDTO getUserToSendEmail(UserDTO user) {
		return (UserDTO) super.sqlMapClientTemplate.queryForObject(SqlName.USER_QUERY_TO_SEND_EMAIL, user);
	}

	@Override
	public FaUserWechatDTO getFaUserWechatDTO(FaUserWechatDTO faUserWechatDTO)
			throws Exception {
		return (FaUserWechatDTO) this.sqlMapClientTemplate.queryForObject(SqlName.QUERY_USERWECHAT_INFO,faUserWechatDTO);
	}

	@Override
	public void  updateUser(UserDTO user) throws Exception {
		if(StringUtils.isBlank(user.getUpdateBy())){
			user.setUpdateBy(StringUtils.isBlank(user.getId())?user.getLoginName():user.getId());
		}
		this.getSqlMapClientTemplate().update(SqlName.FaUserSql.updateFaUser,user);
	}

	@Override
	public void updateUserForce(UserDTO user) throws Exception {
		if(StringUtils.isBlank(user.getUpdateBy())){
			user.setUpdateBy(StringUtils.isBlank(user.getId())?user.getLoginName():user.getId());
		}
		this.getSqlMapClientTemplate().update(SqlName.FaUserSql.updateFaUserForce,user);
		
	}
	@Override
	public String isMobileRegisteredForSFA(String userName) {
		return  (String) super.sqlMapClientTemplate.queryForObject("user.isMobileRegisteredForSFA", userName);
	}

	@Override
	public String isMobileUpdateForSFA(UserDTO user){
		super.sqlMapClientTemplate.update("user.isMobileUpdateForSFA", user);
		return user.getId();
	}

	@Override
	public List<UserDTO> queryFaImage(List<Integer> ids) {
		return this.getSqlMapClientTemplate().queryForList(SqlName.FaUserSql.queryFaImage,ids);
	}
	@Override
	public String updatefaUserInfoAutoPass(UserDTO user) {
		if(StringUtils.isBlank(user.getUpdateBy())){
			user.setUpdateBy(StringUtils.isBlank(user.getId())?user.getLoginName():user.getId());
		}
		 super.sqlMapClientTemplate.update(SqlName.UPDATE_FAUSERINFO_AUTOPASS, user);
		 return user.getId();
	}
}